/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package controlers;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;
import model.Store;

/**
 *
 * @author Tasos
 * O StoresControler parexei yphresies pou aforoun energeies pou ginontai apo tis formes
 * pros ta Katasthmata, symplhrwnei to Control Level osown formwn xrhsimopoioun stoixeia
 * katasthmatwn
 */

public class StoreControler {
private static EntityManager em2;    


public StoreControler(){
 EntityManagerFactory emf = Persistence.createEntityManagerFactory("SuperMarketPU");
        // Dimiourgia Entity Manager
        em2 = emf.createEntityManager();            
}

//epistrefei mia lista me ta diathesima katasthmata
public List getStoreList()
{
//Dhmiourgia Query kai epistrofh ResultList
TypedQuery<Store> allStoresQuery = em2.createNamedQuery("Store.findAll", Store.class);
List<Store> storeResults = allStoresQuery.getResultList();
return storeResults;
}

//epistrefei mia Lista me ta proionta enws sygekriomenou katasthmatos
public List getStoreProducts(String storeID)
{
//Dhmiourgia Query kai epistrofh ResultList
TypedQuery<Store> q1 = em2.createNamedQuery("Store.findByStoreId", Store.class);
q1.setParameter("storeId", Integer.parseInt(storeID));
List<Store> storeList = q1.getResultList();
//to ID einai monadiko ara tha epsitrepsei ena apotelesma
Store store = storeList.get(0);
//Epistrofh ths Listas proiontwn toy katasthmatos
return store.getProductList();

}

//Epistrefei to katasthma Obj pou antistixei sto ID
public Store getStore(String storeID)
{
//Dhmiourgia Query kai epistrofh ResultList
TypedQuery<Store> q1 = em2.createNamedQuery("Store.findByStoreId", Store.class);
q1.setParameter("storeId", Integer.parseInt(storeID));
List<Store> storeList = q1.getResultList();
//to ID einai monadiko ara tha epsitrepsei ena apotelesma
Store store = storeList.get(0);
return store;   
}

}
